<?php

namespace App\Http\Resources\Article\Tab;

use App\Http\Resources\Article\ApiArticleListResource;
use App\Http\Resources\BaseResource;
use App\Models\Article\ArticleCategoryMappingsModel;
use App\Models\Article\ArticleModel;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class CategortyListResource extends BaseResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            "id"           => $this->id,
            "name"         => $this->name,
            "article"      => $this->getArticle(),
            "articleCount" => $this->getArticleCount()
        ];
    }

    /**
     * @return int
     */
    public function getArticleCount(): int
    {
        return ArticleCategoryMappingsModel::query()
                                           ->whereHas("articleRel", function ($query) {
                                               $query->where("status", ArticleModel::STATUS_ON)
                                                     ->whereIn("publishType",
                                                         [
                                                             ArticleModel::PUBLISH_TYPE_TIMING,
                                                             ArticleModel::PUBLISH_TYPE_NOW
                                                         ])
                                                     ->where("effectiveTime", "<=", time());
                                           })
                                           ->where("categoryId", $this->id)
                                           ->count();
    }


    /**
     * @return AnonymousResourceCollection
     */
    public function getArticle(): AnonymousResourceCollection
    {
        $articleIds = ArticleCategoryMappingsModel::query()
                                                  ->whereHas("articleRel", function ($query) {
                                                      $query->where("status", ArticleModel::STATUS_ON)
                                                            ->whereIn("publishType",
                                                                [
                                                                    ArticleModel::PUBLISH_TYPE_TIMING,
                                                                    ArticleModel::PUBLISH_TYPE_NOW
                                                                ])
                                                            ->where("effectiveTime", "<=", time());
                                                  })
                                                  ->where("categoryId", $this->id)
                                                  ->orderBy("sort")
                                                  ->orderByDesc("updatedAt")
                                                  ->orderByDesc("id")
                                                  ->limit(2)
                                                  ->select("articleId")
                                                  ->get()
                                                  ->pluck("articleId")
                                                  ->toArray();

        return ApiArticleListResource::collection(ArticleModel::query()->whereIn("id", $articleIds)->get());
    }
}
